package com.warrior.superdata.util;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

/**
 * @Description:
 * @ClassName: EsSqlUtil.java
 * @date: 2022/3/3 15:00
 * @author xujian
 */
public class EsSqlUtil {

    public static JSONArray getListQuery(RestClient restClient, String sql) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("query", sql);
        Request request = new Request("GET", "_sql");
        request.setJsonEntity(jsonObject.toJSONString(0));
        Response response = restClient.performRequest(request);
        JSONObject apiResultObj = JSONUtil.parseObj(response);
        return apiResultObj.getJSONArray("rows");
    }
}
